package com.mall.user.domain.command;

import com.mall.user.domain.model.Age;
import com.mall.user.domain.model.Email;
import com.mall.user.domain.model.UserName;

/**
 * 创建用户命令
 * 包含创建用户所需的所有信息和业务规则验证
 */
public record CreateUserCommand(
        UserName name,
        Age age,
        Email email
) {
    /**
     * 构造函数中进行业务规则验证
     */
    public CreateUserCommand {
        validateCommand(name, age, email);
    }
    
    /**
     * 验证命令参数的业务规则
     */
    private void validateCommand(UserName name, Age age, Email email) {
        if (name == null) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        if (age == null) {
            throw new IllegalArgumentException("年龄不能为空");
        }
        if (email == null) {
            throw new IllegalArgumentException("邮箱不能为空");
        }
        
        // 业务规则：年龄必须在合理范围内
        if (age.age() < 0 || age.age() > 150) {
            throw new IllegalArgumentException("年龄必须在0-150之间");
        }
        
        // 业务规则：用户名长度限制
        if (name.name().trim().isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空字符串");
        }
        
        if (name.name().length() > 50) {
            throw new IllegalArgumentException("用户名长度不能超过50个字符");
        }
    }
    
    /**
     * 检查命令是否有效
     * @return 是否有效
     */
    public boolean isValid() {
        try {
            validateCommand(name, age, email);
            return true;
        } catch (IllegalArgumentException e) {
            return false;
        }
    }
}
